﻿using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System.Collections.Generic;

namespace demo2
{
    public class SpawnManager :MonoBehaviour
    {

        private static SpawnManager _instance;
        public static SpawnManager Instance()
        {
            if (_instance == null)
            {
                GameObject obj = new GameObject("SpawnManager");
                _instance = obj.AddComponent<SpawnManager>();
                
            }
            return _instance;
        }
        void Awake()
        {
            PreparePrefabs();
        }

        public System.Collections.Generic.Dictionary<string, GameObject> registedPrefabs = new Dictionary<string, GameObject>();
        void PreparePrefabs()
        {
            var obj = Resources.Load("demo2/" + "Player") as GameObject; 
            registedPrefabs.Add("Player", obj);

            var obj1 = Resources.Load("demo2/" + "CubeA") as GameObject;
            registedPrefabs.Add("CubeA", obj1);

            var obj2 = Resources.Load("demo2/" + "SphereA") as GameObject;
            registedPrefabs.Add("SphereA", obj2);


            var obj3 = Resources.Load("demo2/" + "CapsuleB") as GameObject;
            registedPrefabs.Add("CapsuleB", obj3);

            ClientScene.RegisterPrefab(obj);
            ClientScene.RegisterPrefab(obj1);
            ClientScene.RegisterPrefab(obj2);
            ClientScene.RegisterPrefab(obj3);
        }

        public GameObject SpawnPlayer(NetworkConnection conn, short playerControllerId)
        {
            GameObject obj = InstaiatePrefab("Player");
            NetworkServer.AddPlayerForConnection(conn, obj, playerControllerId);
            return obj;
        }
        public GameObject SpawWithClientAuthority(string name,NetworkConnection conn)
        {
            GameObject obj = InstaiatePrefab(name);
            NetworkServer.SpawnWithClientAuthority(obj, conn);
            return obj;
        }

		public GameObject SpawWithClientAuthority(string name,Vector3 _position,Quaternion _q,NetworkConnection conn)
		{
			GameObject obj = InstaiatePrefab(name,_position,_q);
			NetworkServer.SpawnWithClientAuthority(obj, conn);
			return obj;
		}


        public GameObject Spawn(string name)
        {
            GameObject gameObj = InstaiatePrefab(name);

            NetworkServer.Spawn(gameObj);
            return gameObj;
        }

		public GameObject Spawn(string name,Vector3 _position,Quaternion _q)
		{
			GameObject gameObj = InstaiatePrefab(name, _position, _q);
			NetworkServer.Spawn(gameObj);
			return gameObj;
		}


		GameObject InstaiatePrefab(string name)
		{
			return InstaiatePrefab (name,Vector3.zero,Quaternion.identity);
		}

		GameObject InstaiatePrefab(string name,Vector3 _position,Quaternion _q)
        {
            if (!registedPrefabs.ContainsKey(name))
            {
                Debug.LogError(name + " is not registed in client");
                return null;
            }
            var obj = registedPrefabs[name];
			GameObject gameObj = (GameObject)Instantiate(obj, _position, _q);




            return gameObj;
        }
    }
}
